package other.edan.system.k3;

import com.rkhd.platform.sdk.data.model.SyncConfig__c;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.BatchOperateResult;
import com.rkhd.platform.sdk.model.OperateResult;
import org.apache.commons.lang.StringUtils;
import other.edan.system.utils.HCDateUtils;
import other.edan.system.utils.HCXObjectService;
import other.edan.system.utils.TfReturnMsgTools;

import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 同步功能方法
 * @Author: 黎炎松
 * @Date: 2024/8/30 15:31
 */
public class SyncSuper {
    private static final Logger logger = LoggerFactory.getLogger();

    /**
     * 根据配置标识获取配置时间
     * @param syncKey
     * @return
     */
    public Map<String, Object> getParamsMap(String syncKey, String startDateKey, String endDateKey) {
        Map<String, Object> paramsMap = new HashMap<>();

        // 从数据配置表中获取数据
        String sql = String.format("select id,lastSyncTime__c,syncAssistVal__c from syncConfig__c where syncKey__c = '%s'", syncKey);
        SyncConfig__c syncConfig = HCXObjectService.queryOne(sql);
        logger.debug("syncConfig = " + syncConfig);
        if (syncConfig == null) {
            logger.debug(" 数据配置信息不存在");
            throw new RuntimeException("Data configuration information does not exist");
        }

        // 获取同步时间
        String startDate = syncConfig.getLastSyncTime__c();
        String endDate = HCDateUtils.dateToString(new Date(), HCDateUtils.FORMATTIME); // 同步结束时间
        if (StringUtils.isBlank(startDate)) {
            // 如果为空则退回1天前
            startDate = HCDateUtils.getDaysBeforeString(1, HCDateUtils.FORMATTIME);
        }
        // todo
        //startDate = "2023-08-18 00:00:00";
        //endDate = "2024-06-20 00:00:00";
        paramsMap.put(startDateKey, startDate);
        paramsMap.put(endDateKey, endDate);
        return paramsMap;
    }

    /**
     * 获取是或否 1：是 2：否
     * @param str
     * @return
     */
    public Integer getIsYes(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }

        int isYes = 2;
        if (str.equals("是")) {
            isYes = 1;
        }
        return isYes;
    }


    /**
     * 获取时间戳
     * @param dateTimeString
     * @return
     */
    public Long getDateVal(String dateTimeString) {
        if (StringUtils.isBlank(dateTimeString)) {
            return null;
        }
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeString, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        // 转换为时间戳（毫秒）
        return zonedDateTime.toInstant().toEpochMilli();
    }


    /**
     * 更新系统配置
     */
    public void upSysConfig(SyncConfig__c syncConfig, String endDate, int count) {
        syncConfig.setLastSyncTime__c(endDate);
        syncConfig.setLastSyncCount__c(Long.valueOf(count));
        OperateResult update = HCXObjectService.update(syncConfig);
        if (!update.getSuccess()) {
            logger.debug("更新同步配置异常");
            throw new RuntimeException("Update synchronization configuration exception: " + update.getErrorMessage());
        }
    }



}
